/*
 * LinkedList.h
 *
 *  Created on: 26-nov.-2013
 *      Author: Axel
 */

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#ifndef NULL
#define NULL   ((void *) 0)
#endif

//A blueprint for a node
//it contains the previous node and some data
template <class T>
struct Node {
	Node* prev;
	T data;
};

template <class T>
class LinkedList {
public:
	LinkedList(T data) {
		current = new Node<T>();
		current->prev = NULL;
		current->data = data;
	}
	~LinkedList() {
		Node<T>* next;

		while (current->prev != NULL) {
			next = current->prev;
			delete current;
			current = next;
		}

		delete next;
	}

	void addNode(T data) {
		Node<T>* next = new Node<T>();
		next->prev = current;
		next->data = data;

		current = next;
	}

	void deleteNode(int depth) {
		Node<T>* find = current;
		Node<T>* findprev = current;

		while (depth > 0) {
			if (find->prev != NULL) {
				findprev = find;
				find = findprev->prev;
			}
			depth--;
		}

		findprev->prev = find->prev;
		delete find;
	}

	T getNode(int depth) {
		Node<T>* find = current;
		Node<T>* findprev = current;
		while (depth > 0) {
			if (find->prev != NULL) {
				findprev = find;
				find = findprev->prev;
			}
			depth--;
		}
		return find->data;
	}

	T& operator[] (const int depth)	{
		return getNode(depth);
	}

private:
	Node<T>* current;
};

#endif /* LINKEDLIST_H_ */
